package GUI.main.dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;

import officeserver.log_error.UserException;
import officeserver.office.Office;
import officeserver.office.OfficeInterface;
import officeserver.users.AccessLevel;

import GUI.helpers.ButtonType;
import GUI.helpers.PButton;
import GUI.helpers.PTextField;
import GUI.helpers.ProjectConstants;
import GUI.helpers.TextFieldType;
import GUI.main.MainClient;
import GUI.main.navigation.AdminNavigationMenuPanel;

/**
 * @author mramsey3
 * 
 */
public class AddUserDialog extends JDialog implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    // panels...
    private JPanel titlePanel;
    private JPanel centerPanel;
    private JPanel userComboBoxPanel;
    private JPanel firstNamePanel;
    private JPanel lastNamePanel;
    private JPanel usernamePanel;
    private JPanel passwordPanel;
    private JPanel emailPanel;
    private JPanel phonePanel;
    private JPanel agePanel;
    private JPanel buttonPanel;

    // labels...
    private JLabel userComboBoxLabel;
    private JLabel firstNameLabel;
    private JLabel lastNameLabel;
    private JLabel usernameLabel;
    private JLabel passwordLabel;
    private JLabel emailLabel;
    private JLabel phoneLabel;
    private JLabel ageLabel;

    // text fields...
    private JComboBox userComboBox;
    private PTextField firstNameField;
    private PTextField lastNameField;
    private PTextField usernameField;
    private JPasswordField passwordField;
    private PTextField emailField;
    private PTextField phoneField;
    private PTextField ageField;

    // buttons...
    private PButton createButton;
    private PButton cancelButton;

    private final static int WIDTH = 475;
    private final static int HEIGHT = 450;

    private AdminNavigationMenuPanel adminNavigationMenuPanel;

    /**
     * 
     */
    public AddUserDialog(AdminNavigationMenuPanel adminNavigationMenuPanel) {
	this.adminNavigationMenuPanel = adminNavigationMenuPanel;
	setSize(new Dimension(WIDTH, HEIGHT));
	final Toolkit toolkit = Toolkit.getDefaultToolkit();
	final Dimension screenSize = toolkit.getScreenSize();
	final int x = (screenSize.width - WIDTH) / 2;
	final int y = (screenSize.height - HEIGHT) / 2;
	setLocation(x, y);	setLayout(new BorderLayout());

	add(getTitlePanel(), BorderLayout.NORTH);
	add(getCenterPanel(), BorderLayout.CENTER);
	add(getButtonPanel(), BorderLayout.SOUTH);
    }

    /**
     * @return
     */
    private JPanel getTitlePanel() {
	if (titlePanel == null) {
	    titlePanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.SUBHEADER_COLOR);
		    g.fillRect(0, 0, getWidth(), getHeight());
		    g.setColor(Color.white);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
		    g.drawString("add patient:", 24, getHeight() - 8);
		}
	    };
	    titlePanel.setPreferredSize(new Dimension(WIDTH, ProjectConstants.HEADER_HEIGHT));
	}
	return titlePanel;
    }

    /**
     * @return
     */
    private JPanel getCenterPanel() {
	if (centerPanel == null) {
	    centerPanel = new JPanel();

	    centerPanel.setLayout(new BoxLayout(centerPanel,
		    BoxLayout.PAGE_AXIS));

	    centerPanel.add(getUserComboBoxPanel());
	    centerPanel.add(getFirstNamePanel());
	    centerPanel.add(getLastNamePanel());
	    centerPanel.add(getUsernamePanel());
	    centerPanel.add(getPasswordPanel());
	    centerPanel.add(getEmailPanel());
	    centerPanel.add(getPhonePanel());
	    centerPanel.add(getAgePanel());
	}
	return centerPanel;
    }

    /**
     * @return
     */
    private JPanel getUserComboBoxPanel() {
	if (userComboBoxPanel == null) {
	    userComboBoxPanel = new JPanel();

	    userComboBoxPanel.setLayout(new BorderLayout());
	    userComboBoxPanel.setBorder(new EmptyBorder(10,10,0,10));

	    userComboBoxPanel.add(getUserComboBoxLabel(), BorderLayout.WEST);
	    userComboBoxPanel.add(getUserComboBox(), BorderLayout.EAST);
	}
	return userComboBoxPanel;
    }

    /**
     * @return
     */
    private JLabel getUserComboBoxLabel() {
	if (userComboBoxLabel == null) {
	    userComboBoxLabel = new JLabel(ProjectConstants.CELLPAD+"access level:");
	}
	return userComboBoxLabel;
    }

    /**
     * @return
     */
    private JComboBox getUserComboBox() {
	if (userComboBox == null) {
	    AccessLevel[] userTypes = { AccessLevel.PATIENT, AccessLevel.NURSE,
		    AccessLevel.DOCTOR };
	    userComboBox = new JComboBox(userTypes);
	    userComboBox.setPreferredSize(new Dimension(250, 25));
	}
	return userComboBox;
    }

    /**
     * @return
     */
    private JPanel getFirstNamePanel() {
	if (firstNamePanel == null) {
	    firstNamePanel = new JPanel();

	    firstNamePanel.setLayout(new BorderLayout());
	    firstNamePanel.setBorder(new EmptyBorder(10,10,0,10));

	    firstNamePanel.add(getFirstNameLabel(), BorderLayout.WEST);
	    firstNamePanel.add(getFirstNameField(), BorderLayout.EAST);
	}
	return firstNamePanel;
    }

    /**
     * @return
     */
    private JLabel getFirstNameLabel() {
	if (firstNameLabel == null) {
	    firstNameLabel = new JLabel(ProjectConstants.CELLPAD+"first name:");
	}
	return firstNameLabel;
    }

    /**
     * @return
     */
    private PTextField getFirstNameField() {
	if (firstNameField == null) {
	    firstNameField = new PTextField("first name",TextFieldType.LONG);
	}
	return firstNameField;
    }

    /**
     * @return
     */
    private JPanel getLastNamePanel() {
	if (lastNamePanel == null) {
	    lastNamePanel = new JPanel();

	    lastNamePanel.setLayout(new BorderLayout());
	    lastNamePanel.setBorder(new EmptyBorder(10,10,0,10));

	    lastNamePanel.add(getLastNameLabel(), BorderLayout.WEST);
	    lastNamePanel.add(getLastNameField(), BorderLayout.EAST);
	}
	return lastNamePanel;
    }

    /**
     * @return
     */
    private JLabel getLastNameLabel() {
	if (lastNameLabel == null) {
	    lastNameLabel = new JLabel(ProjectConstants.CELLPAD+"last name:");
	}
	return lastNameLabel;
    }

    /**
     * @return
     */
    private PTextField getLastNameField() {
	if (lastNameField == null) {
	    lastNameField = new PTextField("last name",TextFieldType.LONG);
	}
	return lastNameField;
    }

    /**
     * @return
     */
    private JPanel getUsernamePanel() {
	if (usernamePanel == null) {
	    usernamePanel = new JPanel();

	    usernamePanel.setLayout(new BorderLayout());
	    usernamePanel.setBorder(new EmptyBorder(10,10,0,10));

	    usernamePanel.add(getUsernameLabel(), BorderLayout.WEST);
	    usernamePanel.add(getUsernameField(), BorderLayout.EAST);
	}
	return usernamePanel;
    }

    /**
     * @return
     */
    private JLabel getUsernameLabel() {
	if (usernameLabel == null) {
	    usernameLabel = new JLabel(ProjectConstants.CELLPAD+"username:");
	}
	return usernameLabel;
    }

    /**
     * @return
     */
    private PTextField getUsernameField() {
	if (usernameField == null) {
	    usernameField = new PTextField("username",TextFieldType.LONG);
	}
	return usernameField;
    }

    /**
     * @return
     */
    private JPanel getPasswordPanel() {
	if (passwordPanel == null) {
	    passwordPanel = new JPanel();

	    passwordPanel.setLayout(new BorderLayout());
	    passwordPanel.setBorder(new EmptyBorder(10,10,0,10));

	    passwordPanel.add(getPasswordLabel(), BorderLayout.WEST);
	    passwordPanel.add(getPasswordField(), BorderLayout.EAST);
	}
	return passwordPanel;
    }

    /**
     * @return
     */
    private JLabel getPasswordLabel() {
	if (passwordLabel == null) {
	    passwordLabel = new JLabel(ProjectConstants.CELLPAD+"password:");
	}
	return passwordLabel;
    }

    /**
     * @return
     */
    private JPasswordField getPasswordField() {
	if (passwordField == null) {
	    passwordField = new JPasswordField();
	    passwordField.setText("password");
	    passwordField.setPreferredSize(new Dimension(250,25));
	}
	return passwordField;
    }

    /**
     * @return
     */
    private JPanel getEmailPanel() {
	if (emailPanel == null) {
	    emailPanel = new JPanel();

	    emailPanel.setLayout(new BorderLayout());
	    emailPanel.setBorder(new EmptyBorder(10,10,0,10));

	    emailPanel.add(getEmailLabel(), BorderLayout.WEST);
	    emailPanel.add(getEmailField(), BorderLayout.EAST);
	}
	return emailPanel;
    }

    /**
     * @return
     */
    private JLabel getEmailLabel() {
	if (emailLabel == null) {
	    emailLabel = new JLabel(ProjectConstants.CELLPAD+"email:");
	}
	return emailLabel;
    }

    /**
     * @return
     */
    private PTextField getEmailField() {
	if (emailField == null) {
	    emailField = new PTextField("email@email.com",TextFieldType.LONG);
	}
	return emailField;
    }

    /**
     * @return
     */
    private JPanel getPhonePanel() {
	if (phonePanel == null) {
	    phonePanel = new JPanel();

	    phonePanel.setLayout(new BorderLayout());
	    phonePanel.setBorder(new EmptyBorder(10,10,0,10));

	    phonePanel.add(getPhoneLabel(), BorderLayout.WEST);
	    phonePanel.add(getPhoneField(), BorderLayout.EAST);
	}
	return phonePanel;
    }

    /**
     * @return
     */
    private JLabel getPhoneLabel() {
	if (phoneLabel == null) {
	    phoneLabel = new JLabel(ProjectConstants.CELLPAD+"phone number:");
	}
	return phoneLabel;
    }

    /**
     * @return
     */
    private PTextField getPhoneField() {
	if (phoneField == null) {
	    phoneField = new PTextField("phone",TextFieldType.LONG);
	}
	return phoneField;
    }

    /**
     * @return
     */
    private JPanel getAgePanel() {
	if (agePanel == null) {
	    agePanel = new JPanel();

	    agePanel.setLayout(new BorderLayout());
	    agePanel.setBorder(new EmptyBorder(10,10,0,10));

	    agePanel.add(getAgeLabel(), BorderLayout.WEST);
	    agePanel.add(getAgeField(), BorderLayout.EAST);
	}
	return agePanel;
    }

    /**
     * @return
     */
    private JLabel getAgeLabel() {
	if (ageLabel == null) {
	    ageLabel = new JLabel(ProjectConstants.CELLPAD+"age:");
	}
	return ageLabel;
    }

    /**
     * @return
     */
    private PTextField getAgeField() {
	if (ageField == null) {
	    ageField = new PTextField("age",TextFieldType.LONG);
	}
	return ageField;
    }

    /**
     * @return
     */
    private JPanel getButtonPanel() {
	if (buttonPanel == null) {
	    buttonPanel = new JPanel();
	    buttonPanel.setBorder(new EmptyBorder(20,10,10,10));
	    
	    buttonPanel.add(getCreateButton());
	    buttonPanel.add(getCancelButton());
	}
	return buttonPanel;
    }

    /**
     * @return
     */
    private PButton getCreateButton() {
	if (createButton == null) {
	    createButton = new PButton("Create",ButtonType.MEDIUM);
	    createButton.addActionListener(this);
	}
	return createButton;
    }

    /**
     * @return
     */
    private PButton getCancelButton() {
	if (cancelButton == null) {
	    cancelButton = new PButton("Cancel",ButtonType.MEDIUM);
	    cancelButton.addActionListener(this);
	}
	return cancelButton;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
	if (e.getSource() == createButton) {
	    OfficeInterface temp = null;
	    temp = MainClient.getConnection();
	    try {
		temp.addUser(getFirstNameField().getText(), getLastNameField()
			.getText(), getUsernameField().getText(), String
			.valueOf(getPasswordField().getPassword()),
			(AccessLevel) userComboBox.getSelectedItem());
	    } catch (UserException e1) {
		e1.printStackTrace();
	    } /*
	       * catch (IOException e1) { e1.printStackTrace(); } catch
	       * (ClassNotFoundException e1) { e1.printStackTrace(); }
	       */
	    setVisible(false);
	    adminNavigationMenuPanel.refreshPanel();
	} else if (e.getSource() == cancelButton) {
	    setVisible(false);
	}

    }

}
